import subprocess
from files import *


class Installer:
    def __init__(self, resources: str, dist: str) -> None:
        super().__init__()
        self.resources = resources
        self.dist = dist

    def build(self, version='0.17.4'):
        dir_must_exist(self.resources)
        weasel = normalize_path(self.dist, 'weasel')
        delete_dir(weasel)
        copy_dir(normalize_path(self.resources, 'weasel'), weasel)
        copy_dir(normalize_path(self.dist, 'imeasy_dicts'), normalize_path(weasel, 'userdata/imeasy_dicts'))
        copy_dir(normalize_path(self.dist, 'lua'), normalize_path(weasel, 'userdata/lua'))
        copy_file(normalize_path(self.dist, 'imeasy_zrwb86.dict.yaml'),
                  normalize_path(weasel, 'userdata/imeasy_zrwb86.dict.yaml'))
        copy_file(normalize_path(self.dist, 'imeasy_zrwb86.schema.yaml'),
                  normalize_path(weasel, 'userdata/imeasy_zrwb86.schema.yaml'))
        copy_dir(normalize_path(self.resources, 'rime-wubi86-jidian'), normalize_path(weasel, 'rime-wubi86-jidian'))
        if is_file_exist(r'C:\Program Files (x86)\Inno Setup 6\ISCC.exe'):
            subprocess.run(
                [r'C:\Program Files (x86)\Inno Setup 6\ISCC.exe', '/Qp', normalize_path(weasel, 'weasel-setup.iss')],
                check=True,
                capture_output=True,
                text=True
            )
